package com.v1.video.widgets;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.v1.video.util.Logger;


public class MakePhoCompleteTouchView extends ImageView {
	
	
	static final int NONE = 0;
	static final int DRAG = 1;// 拖动中
	static final int ZOOM = 2;// 缩放中
	static final int BIGGER = 3;// 放大ing
	static final int SMALLER = 4;// 缩小ing
	private static final String TAG = "MakePhoCompleteTouchView";
	private int mode = NONE;// 当前的事件

	private float beforeLenght;// 两触点距离
	private float afterLenght;// 两触点距离
	private float scale = 0.04f;// 缩放的比例 X Y方向都是这个值 越大缩放的越快

	private int screenW;
	private int screenH;
	
	public int cutX = 0;//x
	private int cutY = 0;//y
	public int cutW = 0;//宽
	public int cutH = 0;//高
	private int cutB = 0;//右
	private int cutR = 0;//底

	/* 处理拖动 变量 */
	private int start_x;
	private int start_y;
	private int stop_x;
	private int stop_y;
	
//	public final int OFFSETHEIGHT = 40;
	public final int OFFSETHEIGHT = 0;
	
	private int min_mesure_width;
	private int the_cutContainerLeft;
	private int the_holeContainerLeft;
	private ImageView cutiImageView;
	private RelativeLayout holeRelativeLayout;

	private TranslateAnimation trans;// 处理超出边界的动画
	public MakePhoCompleteTouchView(Context arg0, AttributeSet arg1, int arg2) {
		super(arg0, arg1, arg2);
	}
	public MakePhoCompleteTouchView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	public MakePhoCompleteTouchView(Context context) {
		super(context);
	}
	public MakePhoCompleteTouchView(Context context, int w, int h,Bitmap bitmap) {
		super(context);
		this.setPadding(0, 0, 0, 0);
		screenW = w;
		screenH = h;
		this.layout(0, 0, screenW, screenH);
		this.setImageBitmap(bitmap);
//		this.setScaleType(ScaleType.CENTER_CROP);
		this.setScaleType(ScaleType.FIT_CENTER);
	}
	public MakePhoCompleteTouchView(Context context, int w, int h,Bitmap bitmap,int cutX,int cutY,int cutWidth,int cutHeight,int cutRight,
			int cutBottom) {
		super(context);
		this.setPadding(0, 0, 0, 0);
		screenW = w;
		screenH = h;
		this.layout(0, 0, screenW, screenH);
		
		this.setImageBitmap(bitmap);
//		this.setScaleType(ScaleType.CENTER_CROP);
		this.setScaleType(ScaleType.FIT_CENTER);
		this.cutX = cutX;
		this.cutY = cutY;
		this.cutW = cutWidth;
		this.cutH = cutHeight;
		this.cutR = cutRight;
		this.cutB = cutBottom;
	}
	
	public void init(Context context, int w, int h,Bitmap bitmap,int cutX,int cutY,int cutWidth,int cutHeight,int cutRight,
			int cutBottom) {
		this.setPadding(0, 0, 0, 0);
		this.screenW = w;
		this.screenH = h;
		Logger.i(TAG, "screenW11=="+screenW+"screenH111=="+screenH);
//		this.layout(0, 0, screenW, screenH);
		Logger.i(TAG, "bitmap.getwidth="+bitmap.getWidth()+"bitmap.getheight="+bitmap.getHeight());
		this.setImageBitmap(bitmap);
//		this.layout(0, 0, screenW, screenH);
//		setImageViewLayout(bitmap);
//		this.setScaleType(ScaleType.CENTER_CROP);
//		this.setScaleType(ScaleType.CENTER_INSIDE);
		this.setScaleType(ScaleType.FIT_CENTER);
		this.cutX = cutX;
		this.cutY = cutY - OFFSETHEIGHT;
		this.cutW = cutWidth;
		this.cutH = cutHeight;
		this.cutR = cutRight;
//		this.cutB = cutBottom+OFFSETHEIGHT;
		this.cutB = cutBottom;
		this.layout(0, 0, screenW, screenH);
//		postInvalidate();
	}
	
	public void setImageViewLayout(Bitmap bm){
		this.setFrame(0, 0, bm.getWidth(), bm.getHeight());
	}
	
	
	public void setTheMinMesure(int w,int holeLeft,int cutContainerLeft,ImageView cutImageView){
		this.min_mesure_width=w;
		this.the_cutContainerLeft=cutContainerLeft;
		this.the_holeContainerLeft=holeLeft;
		this.cutiImageView=cutImageView;
//		if(getHeight() <= cutiImageView.getHeight() || getWidth() <= cutiImageView.getWidth()){
//            this.layout(0,0, screenW, screenH);
//        }
//		postInvalidate();
	}
	public void setHoleRelativeLayout(RelativeLayout relativeLayout){
		this.holeRelativeLayout=relativeLayout;
	}
	
	/**
	 * 就算两点间的距离
	 */
	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return (float) Math.sqrt(x * x + y * y);
	}

	/**
	 * 处理触碰..
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {				
		
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			mode = DRAG;
			stop_x = (int) event.getRawX();
			stop_y = (int) event.getRawY();
			Logger.i(TAG, "event.getRawY()="+event.getRawY());
			start_x = (int) event.getX();
			start_y = stop_y - this.getTop();
			
			if (event.getPointerCount() == 2)
				beforeLenght = spacing(event);
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			if (spacing(event) > 10f) {
				mode = ZOOM;
				beforeLenght = spacing(event);
			}
			break;
		case MotionEvent.ACTION_UP:
			motionEventActionUpProcess();
			break;
		case MotionEvent.ACTION_POINTER_UP:
			mode = NONE;
			break;
		case MotionEvent.ACTION_MOVE:
			Logger.i(TAG, "ACTION_MOVE_gettop=="+getTop());
			/* 处理拖动 */
			if (mode == DRAG) {
				motionEventActionMoveDragProcess(event);
			}
			/* 处理缩放 */
			else if (mode == ZOOM) {
				motionEventActionMoveZoomProcess(event);
			}
			break;
		}
		return true;
	}
	
//	private ImageView imageViewDragIcon = null;
//	public void setImageView(ImageView imageViewDragIcon){
//		this.imageViewDragIcon = imageViewDragIcon;
//	}
	
	/**最大放大比率(宽)*/
    public static final int maxScaleWidthRate = 3;
    /**最大放大比率(高)*/
    public static final int maxScaleHeightRate = 3;
	public void motionEventActionMoveZoomProcess(MotionEvent event){
		if (spacing(event) > 10f) {
			afterLenght = spacing(event);
			float gapLenght = afterLenght - beforeLenght;
			if (gapLenght == 0) {
				return;
			} else if (Math.abs(gapLenght) > 5f) {
				if (gapLenght > 0) {
					if(this.getHeight() / screenH >= maxScaleHeightRate){
						
					}else if(this.getWidth() / screenW >= maxScaleWidthRate){
						
					}else{
						this.setScale(scale, BIGGER);
					}
				} else {
					this.setScale(scale, SMALLER);
				}
				beforeLenght = afterLenght;
			}
		}
	}
	
	public void motionEventActionMoveDragProcess(MotionEvent event){
		if (Math.abs(stop_x - start_x - getLeft()) < 88
				&& Math.abs(stop_y - start_y - getTop()) < 85) {
			int y = stop_y - start_y;
//			Logger.i(TAG, "moveAction_y="+y);
			this.setPosition(stop_x - start_x, stop_y - start_y, stop_x
					+ this.getWidth() - start_x, stop_y - start_y
					+ this.getHeight());
			stop_x = (int) event.getRawX();
			stop_y = (int) event.getRawY();
		}
	}
	
	public void process(){
	    Logger.i(TAG, "screenW=="+screenW+"screenH=="+screenH);
//	    this.layout(0, 0, screenW, screenH);
//	    postInvalidate();
	}
	
	public void motionEventActionUpProcess(){
		/* 判断是否超出范围 并处理 */
		int disX = 0;
		int disY = 0;
		//下面是我们的图片小于我们给定的视图的时候的效果
		Logger.i(TAG, "motion_111111getHeight="+getHeight());
		Logger.i(TAG, "motion_11111getWidth="+getWidth());
//		if(getHeight() <= cutiImageView.getHeight() || getWidth() <= cutiImageView.getWidth()){
//			this.layout(0, 0, screenW, screenH);
//		}
		Logger.i(TAG, "motion_getHeight="+getHeight());
		Logger.i(TAG, "motion_getWidth="+getWidth());
//		Logger.i(TAG, "motion_bitMapheight="+getDrawingCache().getHeight());
		
		disY = moveOfHeightProcess();
		disX = moveOfWidthProcess();			
		
		if (disX != 0 || disY != 0) {
			trans = new TranslateAnimation(disX, 0, disY, 0);
			trans.setDuration(500);
			this.startAnimation(trans);
		}
		
		mode = NONE;
	}
	
	public int moveOfWidthProcess(){
		int nMoveXSpace = 0;
		if (getWidth() <= screenW) {
			
			if (this.getLeft() < 0) {
				nMoveXSpace = getLeft();
				this.layout(0, this.getTop(), 0 + getWidth(),
						this.getBottom());
			} else if (this.getRight() > screenW) {
				nMoveXSpace = getWidth() - screenW + getLeft();
				this.layout(screenW - getWidth(), this.getTop(), screenW,
						this.getBottom());
			} else{
//				this.layout(0, 0, screenW, screenH);
			}
		}else{
			if (this.getLeft() > 0) {
				nMoveXSpace = getLeft();
				this.layout(0, this.getTop(), 0 + getWidth(),
						this.getBottom());
			} else if (this.getRight() < screenW) {
				nMoveXSpace = getWidth() - screenW + getLeft();
				this.layout(screenW - getWidth(), this.getTop(), screenW,
						this.getBottom());
			}
		}		
		return nMoveXSpace;
	}
	
	//ft_6.25方法内容修改*****换下面的方法
	public int moveOfHeightProcess(){
		int nMoveYSpace = 0;
		if (getHeight() <= screenH ) {
			Logger.i(TAG, "moveOfHeightProcess_this.getTop="+this.getTop());
			Logger.i(TAG, "moveOfHeightProcess_this.cutY="+this.cutY);
			if(this.getTop() > cutY){
//				cutY = cutiImageView.getTop();
				nMoveYSpace = getTop() - cutY;
				this.layout(this.getLeft(), cutY, this.getRight(),
						cutY + this.getHeight());
				
			}else if(this.getBottom() < cutB){
				nMoveYSpace =getBottom() - cutB;
				this.layout(this.getLeft(), cutB - getHeight(),
						this.getRight(), cutB);
			}
//			if (this.getTop() < 0) {
//				nMoveYSpace = getTop();
//				this.layout(this.getLeft(), 0, this.getRight(),
//						0 + this.getHeight());
//			} else if (this.getBottom() > screenH) {
//				nMoveYSpace = getHeight() - screenH + getTop();
//				this.layout(this.getLeft(), screenH - getHeight(),
//						this.getRight(), screenH);				
//			}else{
//				this.layout(0, 0, screenW, screenH);
//			}
		}else{
			if(this.getTop() > cutY){
				nMoveYSpace = getTop() - cutY;
				this.layout(this.getLeft(), cutY, this.getRight(),
						cutY + this.getHeight());
			}else if(this.getBottom() < cutB){
				nMoveYSpace =getBottom() - cutB;
				this.layout(this.getLeft(), cutB - this.getHeight(),
						this.getRight(), cutB);	
			}
//			if (this.getTop() > 0) {
//				nMoveYSpace = getTop();
//				this.layout(this.getLeft(), 0, this.getRight(),
//						0 + this.getHeight());
//			} else if (this.getBottom() < screenH) {
//				nMoveYSpace = getHeight() - screenH + getTop();
//				this.layout(this.getLeft(), screenH - getHeight(),
//						this.getRight(), screenH);				
//			}
		}
		return nMoveYSpace;
	}
	//ft_6.25方法内容修改*****换下面的方法
//	public int moveOfHeightProcess(){
//		int nMoveYSpace = 0;
//		if (getHeight() <= screenH ) {
//			if (this.getTop() < 0) {
//				nMoveYSpace = getTop();
//				this.layout(this.getLeft(), 0, this.getRight(),
//						0 + this.getHeight());
//			} else if (this.getBottom() > screenH) {
//				nMoveYSpace = getHeight() - screenH + getTop();
//				this.layout(this.getLeft(), screenH - getHeight(),
//						this.getRight(), screenH);				
//			}else{
//				this.layout(0, 0, screenW, screenH);
//			}
//		}else{
//			if (this.getTop() > 0) {
//				nMoveYSpace = getTop();
//				this.layout(this.getLeft(), 0, this.getRight(),
//						0 + this.getHeight());
//			} else if (this.getBottom() < screenH) {
//				nMoveYSpace = getHeight() - screenH + getTop();
//				this.layout(this.getLeft(), screenH - getHeight(),
//						this.getRight(), screenH);				
//			}
//		}
//		return nMoveYSpace;
//	}
	//ft_6.25方法内容修改*****换上面的方法
	/**
	 * 实现处理缩放
	 */
	private void setScale(float temp, int flag) {

		if (flag == BIGGER) {
			this.setFrame(this.getLeft() - (int) (temp * this.getWidth()),
					this.getTop() - (int) (temp * this.getHeight()),
					this.getRight() + (int) (temp * this.getWidth()),
					this.getBottom() + (int) (temp * this.getHeight()));
			
		} else if (flag == SMALLER) {
			this.setFrame(this.getLeft() + (int) (temp * this.getWidth()),
					this.getTop() + (int) (temp * this.getHeight()),
					this.getRight() - (int) (temp * this.getWidth()),
					this.getBottom() - (int) (temp * this.getHeight()));
								
		}
	}
	/**
	 * 实现处理拖动
	 */
	private void setPosition(int left, int top, int right, int bottom) {
		this.layout(left, top, right, bottom);
	}
	
	public void setLayout(){
		int scale = 0;
		Logger.i(TAG,"set_cutH="+cutH);
		Logger.i(TAG, "set_getHeight="+this.getHeight());
		Logger.i(TAG, "cutX="+cutX);
		Logger.i(TAG, "cutY="+cutY);
		Logger.i(TAG, "cutW="+cutW);
		Logger.i(TAG, "cutH="+cutH);
//		setScaleType(ScaleType.CENTER_INSIDE);
		this.setScaleType(ScaleType.FIT_CENTER);
		this.layout(0, 0, screenW, screenH);
//		if(cutH > this.getHeight()){
//			scale = cutH / this.getHeight();
//			setLayouScale(scale);
//		}else{
//			this.layout(cutX, cutY, cutR, cutB);
//		}
		
	}	
	private void setLayouScale(int temp){
		this.setFrame(cutX - (int) (temp * this.getWidth()),
				cutY - (int) (temp * this.getHeight()),
				this.getRight() + (int) (temp * this.getWidth()),
				cutB + (int) (temp * this.getHeight()));
		}
	
//	@Override
//	protected void onDraw(Canvas canvas) {
//		// TODO Auto-generated method stub
//		super.onDraw(canvas);
//		Logger.i(TAG, "touchviewOndraw");
//	}
			
}
